package com.glp.work.flow.utils;

import java.util.LinkedHashMap;
import java.util.Map;

import com.glp.work.flow.pojo.SmsIn;

@SuppressWarnings("unused")
public class AssembleDatas {
	
	
//public static void main(String[] args) { 
//  String s = "尊敬的普洛斯融资租赁客户：${parameter1}是您融资租赁合同（${parameter2}、${parameter3}）到期还款日，本期未还金额为：${parameter3}元，总迟延履行金为${parameter4}元。请尽快安排还款，谢谢。如您已还款请忽略此短信/邮件。";
//  String s1 = "尊敬的普洛斯融资租赁客户：2018-06-09是您融资租赁合同（16010058、16010129）到期还款日，本期未还金额为：100185.76元，总迟延履行金为0元。请尽快安排还款，谢谢。如您已还款请忽略此短信/邮件。";
//  
//  AssembleDatas.Assemble(s, s1, false);
//}
    /**
     * 返回Json字符串
     * 
     * @param s
     *            阿里云模板
     * @param s1
     *            短信内容
     * @param ifAtEnd
     *            模板末尾是否有参数 （默认为false）
     * @return Json字符串
     */

    public static String Assemble(String s, String s1, boolean ifAtEnd) {
        // 参数是否在末尾
        char[] ss = s.toCharArray();
        char[] ss1 = s1.toCharArray();
        String[] sArray;
        Map<String, String> mapArray = new LinkedHashMap<String, String>();
        int k = 0;
        int z = 1;
        for (int i = 0; i < ss.length; i++) {
            for (int j = 0; j < ss1.length; j++) {
                if (k > 0) {
	        					if (i + 13 > ss.length) {
	        						continue;
	        					}
	        					if (i + 13 == ss.length) {
	        						mapArray.put("parameter" + z, s1.substring(0));
	        						break;
	        					}                	
                    if (ss[i + 13] == ss1[j]) {
                        mapArray.put("parameter" + z, s1.substring(0, j));
                        s = s.substring(i + 13, s.length());
                        s1 = s1.substring(j, s1.length());
                        System.out.println("s:"+j+":"+s);
                        System.out.println("s1:"+j+":"+s1);
                        ss = s.toCharArray();
                        ss1 = s1.toCharArray();
                        i = 0;
                        j = 0;
                        k = 0;
                        z = z + 1;
                    }
                } else {
                    if (ss[i] != ss1[i]) {
                        if (k == 0) {
                            s = s.substring(i, s.length());
                            s1 = s1.substring(i, s1.length());
                            System.out.println("1:"+i);
                            System.out.println("s:"+i+":"+s);
                            System.out.println("s1:"+i+":"+s1);
                            ss = s.toCharArray();
                            ss1 = s1.toCharArray();
                            i = 0;
                            j = 0;
                            k = 1;
                        }
                    }
                }
            }
        }
        if (ifAtEnd) {
            mapArray.put("parameter" + z, s1);
        } else {
        }
//         Map<String, String> mapArray1 = new HashMap<String, String>();
//         mapArray1.put("riskManualCheckId", "mengguoqing");
//         mapArray1.put("rtxType", "1234567");
        System.out.println("mapArray:"+mapArray);
        return toJson(mapArray);
    }

    /**
     * 返回Json字符串
     * 
     * @param success
     *            返回结果
     * @param jsonMap
     *            需要返回的数据集
     * @return Json字符串
     */
    public static String toJson(Map<String, String> jsonMap) {
        StringBuffer buffer = new StringBuffer();

        buffer.append("{");

        if (jsonMap.size() > 0) {
            // buffer.append(",");
            for (String key : jsonMap.keySet()) {
                if (!key.equals("class")) {
                    if (key.equals("param")) {
                        buffer.append("\"" + key + "\"" + ":" + jsonMap.get(key) + ",");
                    } else {
                        buffer.append("\"" + key + "\"" + ":\"" + jsonMap.get(key) + "\",");
                    }

                }
            }
            // 去掉最后一个','
            buffer.deleteCharAt(buffer.length() - 1);
        }
        buffer.append("}");
        return buffer.toString();
    }

    public static String createParameter(String s, String s1, SmsIn smsIn, boolean ifAtEnd) {

        String paramMap = AssembleDatas.Assemble(s1, s, ifAtEnd);
         // smsIn.setParam(param);
        System.out.println("paramMap："+paramMap);
        Map<String, String> data = new LinkedHashMap<String, String>();

        data.put("accessKeyId", smsIn.getAccessKeyId());
        data.put("accessKeySecret", smsIn.getAccessKeySecret());
        data.put("signName", smsIn.getSignName());
        data.put("templateCode", smsIn.getTemplateCode());
        data.put("mobileNumber", smsIn.getMobileNumber());
        data.put("param", paramMap);
        data.put("type", smsIn.getType());
        return AssembleDatas.toJson(data);
    }
    
   
}
